package ca.concordia.soen6461.wichf.navigation.view;

import static ca.concordia.soen6461.wichf.navigation.view.HomeView.MIN_TIME_SLOT_IN_MINUTES;
import static ca.concordia.soen6461.wichf.navigation.view.HomeView.resetTime;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import ca.concordia.soen6461.wichf.controller.Event;
import ca.concordia.soen6461.wichf.controller.View;
import ca.concordia.soen6461.wichf.entity.User;
import ca.concordia.soen6461.wichf.navigation.view.HomeViewBean.Day;
import ca.concordia.soen6461.wichf.navigation.view.HomeViewBean.TimeSlot;

public abstract class BaseHomeView implements View<HomeViewBean> {

	@Override
	public HomeViewBean render(Event event) {
    	User user = event.getSession().get(User.class);
		DateFormat dateFormat = new SimpleDateFormat("MMM dd"); 
		DateFormat timeFormat = new SimpleDateFormat("HH:mm"); 
	    Calendar date = Calendar.getInstance();
	    Calendar time = Calendar.getInstance();

	    HomeViewBean bean = new HomeViewBean();
	    bean.setViewCartLink(!user.getRoles().contains(User.Role.DOCTOR));
	    
        resetTime(time);
        for (int t = 0; t < 27; t++) {
        	String rowLabel = timeFormat.format(time.getTime());
        	bean.getTimes().add(rowLabel);
            time.add(Calendar.MINUTE, 20);
        }
	    
        resetTime(time);
        for (int d = 0; d < 7; d++) {
            date.add(Calendar.DATE, 1);
            Day day = new Day();
            String columnLabel = dateFormat.format(date.getTime());
			day.setName(columnLabel);
	        for (int t = 0; t < 27; t++) {
	        	TimeSlot slot = new TimeSlot();
				populateSlot(event, user, d, t, slot);
	        	day.getTimeSlots().add(slot);
	            time.add(Calendar.MINUTE, MIN_TIME_SLOT_IN_MINUTES);
	        }
            bean.getDays().add(day);
        }
        
	    return bean;
	}

	protected abstract void populateSlot(Event event, User user, int d, int t, TimeSlot slot);

}
